// pages/category/category/category.js
var util = require('../../../utils/util.js');
var api = require('../../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["默认", "新品", "销售", "价格 ↑"],
    currentTab: 0,
    hidden: false,
    isLogin: wx.getStorageSync("isLogin"),
    navList: [],
    categoriesGoods: [],
    id: "",
    currentCategory: {},
    page: 1,
    pageSize: 12,
    priceIsUp: true,
    hasMoreData: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      isLogin: wx.getStorageSync("isLogin"),
    });
    if (options.id) {
      if (parseInt(options.id) == 5) {
        that.setData({
          id: "",
          currentTab: 1,
          hidden: true
        });
        that.selectProduct("", "", "");
        return false;
      }
      that.setData({
        id: parseInt(options.id)
      });
    }
    // that.getCategoryInfo();
    that.selectProduct("", "", "");
  },

  // getCategoryInfo: function () {
  //   let that = this;
  //   wx.request({
  //     url: api.GoodsCategory,
  //     data: {
  //       page: 1,
  //       per_page: 8,
  //       category: that.data.id
  //     },
  //     method: 'POST',
  //     header: {
  //       'content-type': 'application/json'
  //     },
  //     success: function (res) {
  //       if (res.data.error_code == 0) {
  //         that.setData({
  //           currentCategory: res.data.categories[0]
  //         });
          
  //       } else {
  //         //显示错误信息
  //       }
  //     }
  //   });
  // },

  // getGoodsList: function () {
  //   var that = this;
  //   wx.showLoading({
  //     title: '加载中...',
  //   });
  //   wx.request({
  //     url: api.GoodsList,
  //     data: {
  //       page: that.data.page,
  //       per_page: 12,
  //       category: that.data.id
  //     },
  //     method: 'POST',
  //     header: {
  //       'Content-Type': 'application/json',
  //       'X-bocang-Authorization': wx.getStorageSync('token')
  //     },
  //     success: function (res) {
  //       var goodsListTmp = that.data.categoriesGoods;
  //       if (res.data.error_code == 0) {
  //         if (that.data.page == 1) {
  //           goodsListTmp = [];
  //         }
  //         if (res.data.products.length < that.data.pageSize) {
  //           that.setData({
  //             categoriesGoods: goodsListTmp.concat(res.data.products),
  //             hasMoreData: false,
  //           });
  //         } else {
  //           that.setData({
  //             categoriesGoods: goodsListTmp.concat(res.data.products),
  //             hasMoreData: true,
  //             page: that.data.page + 1,
  //           });
  //         }
  //       }
  //     }
  //   });
  //   setTimeout(function () {
  //     wx.hideLoading();
  //   }, 1000);
  // },

  navbarTap: function(e) {
    let that = this;
    that.setData({
      page: 1
    })
    if (e.currentTarget.id == 0) {
      that.currentTarget("", "", "");
      that.getCurrentTab(e.currentTarget.offsetLeft, e.currentTarget.id)
    }
    if (e.currentTarget.id == 1) {
      that.selectProduct("5", "1", "");
      that.getCurrentTab(e.currentTarget.offsetLeft, e.currentTarget.id)
    }
    if (e.currentTarget.id == 2) {
      that.selectProduct("4", "1", "");
      that.getCurrentTab(e.currentTarget.offsetLeft, e.currentTarget.id)
    }
    if (e.currentTarget.id == 3) {
      var priceIsUp = that.data.priceIsUp;
      if (priceIsUp) {
        that.setData({
          tabs: ["默认", "新品", "销售", "价格 ↑"],
        })
        that.selectProduct("1", "1", "");
      } else {
        that.setData({
          tabs: ["默认", "新品", "销售", "价格 ↓"],
        })
        that.selectProduct("1", "2", "");
      }
      that.setData({
        priceIsUp: !priceIsUp
      });
      that.getCurrentTab(e.currentTarget.offsetLeft, e.currentTarget.id)
    }
  },

  selectProduct: function (sort_key, sort_value, keyword) {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });
    wx.request({
      url: api.Product,
      data: {
        page: that.data.page,
        per_page: 20,
        brand: "",
        category: that.data.id,
        filter_attr: "",
        shop: "",
        keyword: keyword,
        sort_key: sort_key,
        sort_value: sort_value
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json',
        'X-bocang-Authorization': wx.getStorageSync('token')
      },
      success: function (res) {
        var goodsListTmp = that.data.categoriesGoods;
        if (res.data.error_code == 0) {
          if (that.data.page == 1) {
            goodsListTmp = [];
          }
          if (res.data.products.length < that.data.pageSize) {
            that.setData({
              categoriesGoods: goodsListTmp.concat(res.data.products),
              hasMoreData: false,
            });
          } else {
            that.setData({
              categoriesGoods: goodsListTmp.concat(res.data.products),
              hasMoreData: true,
              page: that.data.page + 1,
            });
          }
        }
      }
    })
    setTimeout(function () {
      wx.hideLoading();
    }, 300);
  },

  getCurrentTab: function (offsetLeft, id) {
    let that = this;
    that.setData({
      sliderOffset: offsetLeft,
      currentTab: id
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    that.setData({
      isLogin: wx.getStorageSync("isLogin"),
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      page: 1
    })
    if (that.data.currentTab == 0) {
      that.selectProduct("", "", "");
    }
    if (that.data.currentTab == 1) {
      that.selectProduct("5", "1", "");
    }
    if (that.data.currentTab == 2) {
      that.selectProduct("4", "1", "");
    }
    if (that.data.currentTab == 3) {
      that.selectProduct("1", "1", "");
    }
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    // 页面上拉触底事件的处理函数
    if (that.data.hasMoreData) {
      if (that.data.currentTab == 0) {
        that.selectProduct("", "", "");
      }
      if (that.data.currentTab == 1) {
        that.selectProduct("5", "1", "");
      }
      if (that.data.currentTab == 2) {
        that.selectProduct("4", "1", "");
      }
      if (that.data.currentTab == 3) {
        that.selectProduct("1", "1", "");
      }
    } else {
      wx.showToast({
        title: '没有更多数据',
      })
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  
})